var BasicContent = {

	color: "#F1f1f1",
	edit: function(id){
		// removing buttons
		$(".basicContentEditableOverflow").remove();
		
		var originalText = $("#" + id);
		var offset = $("#" + id).offset();
		
		// creating editbox
		$("body").append("<div id=\"" + id + "-editbox\" class=\"basicContentEditBox\"><form style=\"margin:0;padding:0;border:0px solid red;width:100%;background:transparent;\" onsubmit=\"BasicContent.save('" + id + "'); return false;\"><div id=\"" + id + "-textarea\"></div></form></div>");
		$("#" + id + '-editbox').css({
			width:originalText.innerWidth(),
			minHeight:originalText.outerHeight(),
			position:"absolute",
			top: (offset.top - 1) + 'px',
			zIndex: '13',
			left: (offset.left - 1) + 'px',
			backgroundColor:"#FFFFFF"
			
		});
		// insert original text
		$("#" + id + '-textarea').html(originalText.html());
		
		// show toolbars
		tinyMCE.execCommand('mceAddControl',false,id + '-textarea');
        
		// append buttons
		$("#" + id + '-editbox').append("<input type=\"submit\" value=\"Save\" class=\"button-ok\" onclick=\"BasicContent.save('" + id + "')\"/>&nbsp;<input  class=\"button-neutral\"  type=\"submit\" value=\"Cancel\" onclick=\"BasicContent.cancel('" + id + "')\"/>");
	},
	
	cancel: function(id){
		tinyMCE.execCommand('mceRemoveControl',false,id + '-textarea');
		$("#" + id + "-editbox").remove();
	},
	
	save: function(id){
	
		// force tinymce to empty buffer
		tinyMCE.triggerSave();
		
		var newText = $("#" + id + '-textarea').html();
		
		//save
		$("#" + id).html(newText);
		Cyfy.ajax("BasicContent", "ajaxSave", "", "bcid=" + $("#" + id + "-id").val() + "&content=" + Url.encode(newText));
		
		BasicContent.cancel(id);
	},
    
    
    focus:function(x, y)
    {
        tinyMCE.activeEditor.focus();
    },
    
    
	
	del: function(id){
		// removing buttons
		$(".basicContentEditableOverflow").remove();
		Cyfy.ajax("BasicContent", "deleteContent", "", "bcid=" + $("#" + id + "-id").val() ,"Cyfy.refreshPage();");
    }
    

}

$(document).ready(function()
{
	// COLOR BORDER && BUTTONS
	$(".basicContentEditable").css({
		position: "relative"
	});
	$(".basicContentEditable a").css({
		position: "relative",
		zIndex:12
	});
	$(".basicContentEditable").hover(function(){
        var del = "";
        
        //enable delete?
        if(!$(this).hasClass('noDelete')){
            del = "<input type=\"submit\" value=\"Delete\" onclick=\"BasicContent.del('" + $(this).attr("id") + "'); return false;\" />"
        }
        
        // overlay
		$(this).append("<div class=\"basicContentEditableOverflow\"><input type=\"submit\" value=\"Edit\" onclick=\"BasicContent.edit('" + $(this).attr("id") + "'); return false;\" /> <input type=\"submit\" value=\"Classic Edit\" onclick=\"Cyfy.redirectTo('administration/BasicContent/blocksList/" + $("#" +  $(this).attr("id") + "-id").val()  + "'); return false;\" />" + del + "</div>");
		$(".basicContentEditableOverflow").fadeTo(0,0);
		$(".basicContentEditableOverflow").fadeTo(200,0.7);
		$(".basicContentEditableOverflow").css({
			position:"absolute",
			top:"0px",
			left:"0px",
			backgroundColor: BasicContent.color,
			zIndex: '11',
			width: ($(this).innerWidth()),
			height: ($(this).innerHeight() - 20)
		});
        // dblclick edit
        var bc = this;
        $(".basicContentEditableOverflow").dblclick(function(e){
            BasicContent.edit($(bc).attr("id"));
            // focus
            setTimeout('try{ BasicContent.focus('+ e.pageX + ', '+ e.pageY +'); } catch(e){}', 2000);
        });
	}, function(){
		$(".basicContentEditableOverflow").remove();
	});
});
